<template>
	<view class="login">
		<view class="center">
			<!-- #ifdef APP -->
			<button class="btn" @click="onIosLogin" v-if="platform == 'ios'">
				<image class="ii" src="/static/img/login_apple.png"></image>
				苹果登陆
			</button>
			<button class="btn" @click="onAndroidWxLogin" v-if="platform == 'android'">
				<!-- <image class="ii" src="/static/img/login_apple.png"></image> -->
				微信登陆
			</button>
			<!-- #endif -->
			<!-- #ifdef MP-WEIXIN -->
			<button class="btn" @click="onWxLogin">
				<!-- <image class="ii" src="/static/img/login_apple.png"></image> -->
				微信登陆
			</button>
			<!-- #endif -->
			<!-- #ifdef MP-ALIPAY -->
			<button class="btn" @click="onZhiLogin">
				<!-- <image class="ii" src="/static/img/login_apple.png"></image> -->
				支付宝登陆
			</button>
			<!-- #endif -->
			<!-- #ifdef MP-QQ -->
			<button class="btn" @click="onQQLogin">
				<!-- <image class="ii" src="/static/img/login_apple.png"></image> -->
				QQ登陆
			</button>
			<!-- #endif -->
			<view class="v_yins">
				<checkbox-group @change="checkboxGroup">
					<label>
						<!-- :checked="uniPlatform != 'mp-weixin'" -->
						<checkbox value="cb":checked="isSureRadio"  style="transform:scale(0.7)"/>
					</label>
				</checkbox-group>
				<text class="tt">请同意</text>
				<navigator url="/src/pages/Agreement/serviceAgreement" class="tt ss">用户服务协议</navigator>
				<text class="tt">和</text>
				<navigator url="/src/pages/Agreement/privacyAgreement" class="tt ss">隐私政策条款</navigator>
			</view>
		</view>
		
		<uni-popup ref="popup" type="bottom" border-radius="20px 20px 0 0">
			<view class="popupXieyi">
				<uni-icons class="closepopUp" @click="closePOPIcon" type="closeempty" color="#333" size="24"></uni-icons>
				<view style="font-size:30rpx;text-align: center;margin-top:40rpx;">
					我已阅读并同意 
					<navigator style="color: #7E8BFF;" url="/src/pages/Agreement/serviceAgreement" class="tt ss">用户服务协议</navigator>
					<text class="tt">和</text>
					<navigator style="color: #7E8BFF;" url="/src/pages/Agreement/privacyAgreement" class="tt ss">隐私政策条款</navigator>
				</view>				
				<button size="default" type="default" @click="closePOP">继续</button>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		getUser,
		wxminilogin
	} from '@/src/utils/utils.js'
	export default {
		data() {
			return {
				platform: null,
				tongyi: false,
				uniPlatform:'',
				isSureRadio:false
			};
		},
		onLoad() {
			this.platform = getApp().globalData.platform
			this.uniPlatform = uni.getSystemInfoSync().uniPlatform;
		},
		methods: {
			popupOpen(){
				// 通过组件定义的ref调用uni-popup方法 ,如果传入参数 ，type 属性将失效 ，仅支持 ['top','left','bottom','right','center']
				this.$refs.popup.open('bottom')
			},
			closePOP(){
				this.$refs.popup.close();				
				this.isSureRadio=true;	`	`
			},
			closePOPIcon(){
				this.$refs.popup.close();		
			},
			checkboxGroup(){
				this.isSureRadio = !this.isSureRadio;
			},
			async onQQLogin(){
				try {
					const [fail, success] = await uni.login({
						provider: 'qq',
					});
					console.log(fail, success)
					let result = await getUser({
						code: success.code,
						pid:uni.getStorageSync('pid')
					}, true)
					this.$showToast('登陆成功')
					uni.navigateBack()
				} catch (e) {
					if (!e) return
					uni.showModal({
						content: e.errMsg || e.message,
						showCancel: false
					});
				}
			},
			async onAndroidWxLogin() {
				try {
					if(!this.isSureRadio){
						this.popupOpen()
						return;
					}
					
					const [fail, success] = await uni.login({
						provider: 'weixin',
						"onlyAuthorize": true, // 微信登录仅请求授权认证
					});
					console.log(fail, success)
					let result = await getUser({
						code: success.code,
						pid:uni.getStorageSync('pid')
					}, true)
					this.$showToast('登陆成功')
					uni.navigateBack()
				} catch (e) {
					if (!e) return
					uni.showModal({
						content: e.errMsg || e.message,
						showCancel: false
					});
				}
			},
			async onWxLogin() {
				try {
					if(!this.isSureRadio){
						this.popupOpen()
						return;
					}
					const [fail, success] = await uni.login({
						provider: 'weixin'
					});
					let result = await getUser({
						code: success.code,
						pid:uni.getStorageSync('pid')
					}, true)
					this.$showToast('登陆成功')
					uni.navigateBack()
				} catch (e) {
					if (!e) return
					uni.showModal({
						content: e.errMsg || e.message,
						showCancel: false
					});
				}
			},
			async onIosLogin() {
				try {
					const [fail, success] = await uni.login({
						provider: 'apple'
					});
					let result = await getUser({
						openid: success.authResult.openid,
						pid:uni.getStorageSync('pid')
					}, true)
					this.$showToast('登陆成功')
					uni.navigateBack()
				} catch (e) {
					if (!e) return
					uni.showModal({
						content: e.errMsg || e.message,
						showCancel: false
					});
				}
			},
			async onZhiLogin() {
				try {
					const [fail, success] = await uni.login({
						scopes: 'auth_base'
					});
					console.log(success.authCode)
					let result = await getUser({
						code: success.authCode
					}, true)
					this.$showToast('登陆成功')
					uni.navigateBack()
				} catch (e) {
					if (!e) return
					uni.showModal({
						content: e.errMsg || e.message,
						showCancel: false
					});
				}
			}
		}
	}
</script>

<style lang="scss">
	@import "login.scss";
</style>